﻿using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Jinndev.UI;

namespace Jinndev {

    public class InputFileField : BaseUIBehaviour {

        public abstract class AbstractOption {
            public string label;
            public string placeholder;
            public string initPath;
            public System.Action<string> onPathChanged;
            public bool hidePaste;
            public bool hideBrowse;
            public bool hideClear;


            public AbstractOption(string label, System.Action<string> onPathChanged) {
                this.label = label;
                this.onPathChanged = onPathChanged;
            }

            public AbstractOption(string label, string placeholder, System.Action<string> onPathChanged) {
                this.label = label;
                this.placeholder = placeholder;
                this.onPathChanged = onPathChanged;
            }

        }

        public class FileOption : AbstractOption {
            public string[] exts;

            public FileOption(string label, System.Action<string> onPathChange, params string[] exts) : base(label, onPathChange) {
                this.exts = exts;
            }
            public FileOption(string label, string placeholder, System.Action<string> onPathChange, params string[] exts) : base(label, placeholder, onPathChange) {
                this.exts = exts;
            }
        }

        public class DirectoryOption : AbstractOption {
            public DirectoryOption(string label, System.Action<string> onPathChange) : base(label, onPathChange) {

            }
            public DirectoryOption(string label, string placeholder, System.Action<string> onPathChange) : base(label, placeholder, onPathChange) {

            }
        }

        #region static
        public static readonly string PATH = "Prefab/UI/Common/InputFileField";

        public static InputFileField Create(Transform parent, AbstractOption option) {
            var com = CommonUtil.Instantiate<InputFileField>(PATH, parent);
            com.Init(option);
            return com;
        }
        #endregion

        public InputField inputField;
        public Text textPlaceholder;
        public Text textLabel;

        private AbstractOption option;

        private void Init(AbstractOption option) {
            this.option = option;

            inputField.onValueChanged.AddListener(text => {
                option.onPathChanged?.Invoke(text);
            });

            if (option.label != null) {
                textLabel.text = option.label;
            }
            if (option.placeholder != null) {
                textPlaceholder.text = option.placeholder;
            }

            if (option.initPath != null) {
                inputField.text = option.initPath;
            }

            CommonUtil.SetActive(transform, "ButtonBrowse", !option.hideBrowse);
            CommonUtil.SetActive(transform, "ButtonPaste", !option.hidePaste);
            CommonUtil.SetActive(transform, "ButtonClear", !option.hideClear);


            if (option is FileOption) {
                var fileOption = option as FileOption;
                Util.InitBrowsePasteFile(inputField, "选择" + textLabel.text, fileOption.exts);
            }
            else {
                var dirOption = option as DirectoryOption;
                Util.InitBrowsePasteDirectory(inputField, "选择" + textLabel.text);
            }
        }

        public void SetPath(string path, bool notify) {
            if (notify) {
                inputField.text = path;
            }
            else {
                inputField.SetTextWithoutNotify(path);
            }
        }

        public string GetPath() {
            return inputField.text;
        }

    }

}
